/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.controls;

import cn.ac.nercis.pes.common.constant.project.FinishState;
import cn.ac.nercis.pes.community.model.vo.MenuVO;
import cn.ac.nercis.pes.community.utils.IconUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TreeCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 树节点渲染类
 * @author zpy
 * @version 1.0.0
 */
public class TreeListCell extends TreeCell<MenuVO> {

    private final HBox nodeBox = new HBox();
    private final Label nodeLabel = new Label();
    private final Label nodeState = new Label();
    private final Integer MAX_SHOW_LENGTH;
    private final boolean enableState;
    private String icon;

    public TreeListCell(){
        this(50,false);
    }

    public TreeListCell(Integer maxShowLength,Boolean enableState){
        MAX_SHOW_LENGTH = maxShowLength;
        this.enableState = enableState;
        nodeBox.getChildren().add(nodeLabel);
        nodeBox.getStyleClass().add("pes-tree-node");
        nodeLabel.getStyleClass().add("tree-node-text");
        if(enableState){
            nodeBox.setSpacing(10);
            nodeBox.setPadding(new Insets(0,5,0,0));
            HBox stateBox = new HBox();
            stateBox.setAlignment(Pos.CENTER_RIGHT);
            stateBox.getChildren().add(nodeState);
            HBox.setHgrow(stateBox, Priority.SOMETIMES);
            nodeBox.getChildren().add(stateBox);
        }
        setGraphic(nodeBox);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }

    @Override
    protected void updateItem(MenuVO item, boolean empty) {
        super.updateItem(item, empty);
        if(Objects.nonNull(item)){
            if(Objects.isNull(getGraphic())){
                setGraphic(nodeBox);
            }
            setNodeText(item.getTitle(),item.getIcon(),item.getToolTip());
            if(this.enableState){
                if(item.getState() == FinishState.FINISH){
                    nodeState.setText("已完成");
                    if(!nodeState.getStyleClass().contains("item-finish-state")){
                        nodeState.getStyleClass().clear();
                        nodeState.getStyleClass().add("item-finish-state");
                    }
                }else if(item.getState() == FinishState.INCOMPLETE){
                    nodeState.setText("未完成");
                    if(!nodeState.getStyleClass().contains("item-incomplete-state")){
                        nodeState.getStyleClass().clear();
                        nodeState.getStyleClass().add("item-incomplete-state");
                    }
                }else{
                    nodeState.setText("未填写");
                    if(!nodeState.getStyleClass().contains("item-none-state")){
                        nodeState.getStyleClass().clear();
                        nodeState.getStyleClass().add("item-none-state");
                    }
                }
            }
        }else{
            setText(null);
            setGraphic(null);
        }
    }

    private void checkShowLength(Label label,String name){
        if(StringUtils.length(name) > MAX_SHOW_LENGTH){
            label.setText(StringUtils.substring(name,0,MAX_SHOW_LENGTH).concat("..."));
            label.setTooltip(new Tooltip(name));
        }
    }

    private void setNodeText(String name, String icon,String toolTip){
        nodeLabel.setText(name);
        if(!StringUtils.equals(this.icon,icon) && StringUtils.isNotBlank(icon)){
            this.icon = icon;
            nodeLabel.setGraphic(IconUtils.getGlyph(icon,"tree-item-icon"));
            nodeLabel.setGraphicTextGap(10);
        }
        this.checkShowLength(nodeLabel,name);
        if(StringUtils.isNotBlank(toolTip)){
            nodeLabel.setTooltip(new Tooltip(toolTip));
        }
    }
}